package co.yiiu.pybbs.util;

import cn.stylefeng.roses.file.FileOperator;
import co.yiiu.pybbs.model.SysFileInfo;
import co.yiiu.pybbs.service.ISystemConfigService;
import co.yiiu.pybbs.service.SysFileInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

/**
 * Created by tomoya.
 * Copyright (c) 2018, All Rights Reserved.
 * https://yiiu.co
 */
@Component
public class FileUtil {

    private Logger log = LoggerFactory.getLogger(FileUtil.class);

    @Autowired
    private SysFileInfoService sysFileInfoService;

    /**
     * 上传文件
     *
     * @param file 要上传的文件对象
     * @return 文件的id
     */
    public String upload(MultipartFile file, String fileName, String customPath) {

        // 上传文件到oss
        SysFileInfo sysFileInfo = sysFileInfoService.uploadFile(file);

        // 返回oss的地址，默认一小时过期
        return "https://guns-bbs.oss-cn-beijing.aliyuncs.com/" + sysFileInfo.getFileObjectName();
    }
}
